package com.cskaoyan.gateway.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.user.UserLoginForm;
import com.cskaoyan.gateway.form.user.UserRegisterForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: DHY
 * @Date: 2021/1/22 18:35
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class AuthController {
    @Reference(timeout = 3000,check = false)
    IKaptchaService kaptchaService;
    @Reference(timeout = 3000,check = false)
    ILoginService loginService;


    /**
     * 验证用户登录
     * @return
     */
    @GetMapping("login")
    public ResponseData login(HttpServletRequest request) {
        String userInfo = (String) request.getAttribute("userInfo");
        JSONObject object = JSON.parseObject(userInfo);
        String uName = (String) object.get("uName");
        UserLoginInfoResponse response= loginService.returnUserInfo(uName);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());


    }

    /**
     * 用户登录
     * @param loginForm
     * @return
     */
    @PostMapping("login")
    @Anoymous
    public ResponseData userLogin(@RequestBody UserLoginForm loginForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        //验证验证码
        KaptchaCodeResponse response = getKaptchaCodeResponse(loginForm.getCaptcha(), httpServletRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            //验证码校验成功,验证用户信息
            UserLoginRequest userLoginRequest = new UserLoginRequest();
            userLoginRequest.setUserName(loginForm.getUserName());
            userLoginRequest.setPassword(loginForm.getUserPwd());
            //验证用户账号密码
            UserLoginResponse userLoginResponse = loginService.validUserLogin(userLoginRequest);
            if (userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
                //cookie里放入token信息
                Cookie cookie = CookieUtil.genCookie("access_token", userLoginResponse.getToken(), "/", 60 * 60*24 );
                httpServletResponse.addCookie(cookie);
                return new ResponseUtil().setData(userLoginResponse);
            }
            return new ResponseUtil().setErrorMsg(userLoginResponse.getMsg());

        }
        //验证码校验失败,直接返回
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 验证验证码
     * @param
     * @param httpServletRequest
     * @return
     */
    private KaptchaCodeResponse getKaptchaCodeResponse(String code, HttpServletRequest httpServletRequest) {
        KaptchaCodeRequest request = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        request.setUuid(uuid);
        request.setCode(code);
        return kaptchaService.validateKaptchaCode(request);
    }

    /**
     * 用户注销
     * @return
     */
    @GetMapping("loginOut")
    public ResponseData userLoginOut(HttpServletResponse httpServletResponse) {
        Cookie cookie = CookieUtil.genCookie("access_token", "", "/", 0 );
        httpServletResponse.addCookie(cookie);
        return new ResponseUtil().setData(null);

    }

    /**
     * 用户注册
     * @return
     */
    @PostMapping("register")
    @Anoymous
    public ResponseData userRegister(@RequestBody UserRegisterForm userRegisterForm,HttpServletRequest httpServletRequest) {
        //校验验证码
        KaptchaCodeResponse response = getKaptchaCodeResponse(userRegisterForm.getCaptcha(), httpServletRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            //验证码校验成功,验证用户信息
            UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
            userRegisterRequest.setUserName(userRegisterForm.getUserName());
            userRegisterRequest.setUserPwd(userRegisterForm.getUserPwd());
            userRegisterRequest.setEmail(userRegisterForm.getEmail());
            UserRegisterResponse userRegisterResponse= loginService.userRegister(userRegisterRequest);
            if (userRegisterResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
                return new ResponseUtil().setData(userRegisterResponse);
            }
            return new ResponseUtil().setErrorMsg(userRegisterResponse.getMsg());
        }
        //验证码不正确
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 邮箱激活
     * @return
     */
    @GetMapping("verify")
    @Anoymous
    public String emailVerify(String userName,String uuid) {
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUserName(userName);
        userVerifyRequest.setUuid(uuid);
        UserVerifyResponse response= loginService.emailVerify(userVerifyRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return "<h2>激活成功,快去登录吧...</h2>";
        }
        return "<h2>"+response.getMsg()+"</h2>";


    }

}
